Thread: [HELP]C Programming , Stack conversion from infix to prefix

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    1

    [HELP]C Programming , Stack conversion from infix to prefix

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<ctype.h>
    
    
    #define MAX 50
    
    
    
    
    
    
    char output[MAX];
    char stack[MAX];
    char input[MAX];
    char *s,*t;
    char ch;
    int top;
    int l;
    int opr;
    
    
    
    
    void initialize(void);
    void SetExpression(char*);
    int PopFromStack(char);
    int priority(char);
    void ConvertToPrefix(void);
    void ConvertToPostfix(void);
    main (void)
    
    
    
    
    {
         initialize();
         printf("\nEnter an expression in infix form:");
         gets(input);
         printf("\nSpecify output expression type,1)Prefix 2)Postfix");
         ch=getch();
         SetExpression(input);
         if(ch=='1')
         {
                    strrev(s);
                    ConvertToPrefix();
                    strrev(output);
                    printf("\nThe Prefix expression is:");
                    }
                    else
                    {
                        ConvertToPostfix();
                        printf("\nThe Postfix expression is:");
                        }
                        *(t)=opr;
                        t++;
                        opr=
                        PopFromStack();
                        }          
                    PushOnStack((opr);
                    PushOnStack((*(s));
                    }
         else PushOnStack(*(s));
         s++;
         }
         if(*(s)=='(')
         {
                      opr=PopFromStack();
                      while(opr!=')')
                      {
                      opr=PopFromStack();
                      }
                      s++;
                      }
         }
         while(top!=-1)
         {
                       opr=PopFromStack();
                       *(t)=opr;
                       t++;
         }
         t++;
         }
             char opr;
             while(*(s))
             {
                           if(*(s))==''||*(s)=='\t')
                           {
                           s++;
                           continue;
                           }
                           if(isdigit(*(s))||isalpha(*(s)))
                           {
                           while(isdigit(*(s))||isalpha(*
                           (s)))
                           {
                                *(t)=*(s);
                                s++;
                                t++;
                           }
             }
             if(*(s)=='(')
             {
                          PushOnStack(*(s));
                          s++;
             }
             if(*(s)=='*'||*(s)=='+'||*(s)
             =='/'||*(s)=='%'||*(s)=='-'||*(s)
             =='^')
             {
                   if(top!=-1)
                   {
                              opr=PopFromStack();
                              while(priority(opr)>=
                              priority(*(s)))
                              {
                                             *(t)=opr;
                                             t++;
                                             opr=
                                             PopFromStack();
                              }
                              PushOnStack(opr);
                              PushOnStack(*(s));
                              }
                              else PushOnStack(*(s));
                              s++;
                              }
                              if(*(s)==')')
                              {
                                           opr=PopFromStack();
                                           while(opr!='(')
                                           {
                                           *(t)=opr;
                                           t++;
                                           opr=PopFromStack();
                              }
                              s++;
                              }
                              while(top!=-1)
                              {
                              opr=PopFromStack();
                              *(t)=opr();
                              t++;
                              }
                              t++;
                              }
                              getch();
                              }
    Please help me , with some errors .

    http://i44.tinypic.com/11cfx28.png

    my output should look like this ..

    Enter an expression in infix form: (9+6)/(2*4)^15

    Specify output epression type,1) Prefix 2)Postfix
    The Prefix expression is: /+96^*2415


    ----------------------------------------------

    Enter an expression in infix form: (9+6)/(2*4)^15

    Specify output epression type,1) Prefix 2)Postfix
    The Postfix expression is:96+24*15^/

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It seems you've copied this code and don't understand it yourself.
    Looks quite similar to this, for example.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Could you tell us what algorithm you're using?
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. infix to postfix using stack Conversion
    By danishzaidi in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2013, 08:41 AM
  2. Infix to Prefix
    By ThLstN in forum C Programming
    Replies: 2
    Last Post: 04-17-2008, 05:11 AM
  3. Infix to prefix conversion
    By bbanelli in forum C Programming
    Replies: 1
    Last Post: 11-19-2007, 02:17 PM
  4. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  5. infix to prefix.....damn my eyes are fried
    By misplaced in forum C++ Programming
    Replies: 10
    Last Post: 10-04-2004, 01:36 AM